//
//  RegViewController.m
//  jinwang
//
//  Created by jzb on 2018/3/1.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "RegViewController.h"
#import "RegCell.h"
#import "AgreementViewController.h"
#import "BirthdayVC.h"
#import "JWAlertChoseButsView.h"
#import "HomeAddressVC.h"
#import "HSFLocationVC.h"

#define KTime   60//设置重新发送的时间  自己可以改

@interface RegViewController ()<HSFLocationVCDelegate>
{
    NSTimer *_myTimer;//定时器
    TYAlertController *_alertController;
    NSString *_user_sex;//性别（0男1女）
    NSString *_user_birthday;//生日（1996-02-06）
    NSString *_user_occupation;//职业
    NSString *_user_duties;//职务
    NSString *_user_business;//业务范围
    NSString *_home_region_id;//家乡地区id
    NSString *_home_address;//家乡详细地址
    NSString *_region_name;//当前城市名称
}
@property (nonatomic,strong) RegCell *cell;
@property (nonatomic,assign) NSInteger timer;


@end

@implementation RegViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"注册";
    [self TYAlertBuild];
    [self setUpTableView];
     _timer = KTime;
    _user_sex = @"0";
    
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    
    [self registerCellWithCellClassArray:@[NSStringFromClass([RegCell class])]];
    
}

- (void)TYAlertBuild{
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth - 80, 180)];
    view.backgroundColor = [UIColor whiteColor];
    
    view.layer.cornerRadius = 8.0;
    view.layer.masksToBounds = YES;
    
    UILabel *blackLabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 20, view.width - 20, 50)];
    
    blackLabel.text = @"使用此软件须同意《今望APP用户使用协议》";
    
    blackLabel.numberOfLines = 0;
    
    blackLabel.font = [UIFont systemFontOfSize:15];
    [view addSubview:blackLabel];
    
//    blackLabel.backgroundColor = [UIColor cyanColor];
    
    UIButton *xieyiBut =  [[UIButton alloc]initWithFrame:CGRectMake(10, 70, view.width - 20,30)];
    [xieyiBut setTitle:@"查看《今望APP用户使用协议》" forState:UIControlStateNormal];
    [xieyiBut setTitleColor:kRGBColor(89, 176, 254) forState:UIControlStateNormal];
    xieyiBut.titleLabel.font = [UIFont systemFontOfSize:15];
    xieyiBut.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [xieyiBut addTarget:self action:@selector(xieyitiaokuanbuttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
    xieyiBut.tag = 1003;
    [view addSubview:xieyiBut];
    
    UIView *hengline = [[UIView alloc]initWithFrame:CGRectMake(0, view.height - 51, kScreenWidth - 80, 1)];
    hengline.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:hengline];
    UIView *shuline = [[UIView alloc]initWithFrame:CGRectMake(view.width/2-0.5, view.height - 50, 1, 50)];
    shuline.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:shuline];
    
    UIButton *quxiaobutton = [[UIButton alloc]initWithFrame:CGRectMake(0, view.height - 50, view.width/2-0.5,50)];
    [quxiaobutton setTitle:@"取消" forState:UIControlStateNormal];
    [quxiaobutton setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    [quxiaobutton addTarget:self action:@selector(xieyitiaokuanbuttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
    quxiaobutton.tag = 1001;
    [view addSubview:quxiaobutton];
    
    UIButton *quedingbutton = [[UIButton alloc]initWithFrame:CGRectMake(view.width/2-1, view.height - 50, view.width/2-0.5,50)];
//    quedingbutton.backgroundColor = [UIColor orangeColor];
    [quedingbutton setTitle:@"确认" forState:UIControlStateNormal];
    [quedingbutton setTitleColor:kRGBColor(89, 176, 254) forState:UIControlStateNormal];
    quedingbutton.tag = 1002;
    [quedingbutton addTarget:self action:@selector(xieyitiaokuanbuttonOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:quedingbutton];
    
    _alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
    
    [self presentViewController:_alertController animated:YES completion:nil];
    
}

- (void)xieyitiaokuanbuttonOnclick:(UIButton *)sender{
    
    [_alertController.view hideView];
    if (sender.tag == 1001) {
        
        [self goBack];
        
    }else if (sender.tag == 1003){
        
        AgreementViewController *AVC = [AgreementViewController new];
        
        [self.navigationController pushViewController:AVC animated:YES];
        
    }
}

#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    self.cell = [tableView dequeueReusableCellWithIdentifier:[RegCell getCellReuseIdentifier]];
    self.cell.contentView.backgroundColor = [UIColor clearColor];
    
    [self.cell.GetCodeBut addTarget:self action:@selector(GetCodeButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.user_sex_manBtn addTarget:self action:@selector(user_sexBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.user_sex_womanBtn addTarget:self action:@selector(user_sexBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.BirthdBtn addTarget:self action:@selector(user_birthdayBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.ProfessionalBut addTarget:self action:@selector(user_ProfessionalButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.PostBut addTarget:self action:@selector(user_ProfessionalButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.BusinessBut addTarget:self action:@selector(user_ProfessionalButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.HomeAddressBtn addTarget:self action:@selector(user_ProfessionalButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.LocaBtn addTarget:self action:@selector(user_ProfessionalButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    [self.cell.RegBut addTarget:self action:@selector(RegButOnclick:) forControlEvents:UIControlEventTouchUpInside];
    
    
    return self.cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 980;
}
- (void)GetCodeButOnclick:(UIButton *)sender{
    
    if (kStringIsEmpty(self.cell.PhoneTF.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }
    else if (![NSString IsPhoneNumber:self.cell.PhoneTF.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }
    else{
        [self getCode:self.cell.PhoneTF.text currentBtn:sender];
    }
    
}
- (void)user_sexBtnOnclick:(UIButton *)sender{
    
    UIButton *manbutton = (UIButton *)[self.view viewWithTag:1004];
    [manbutton setImage:[UIImage imageNamed:@"jw_sex_btn.png"] forState:UIControlStateNormal];
    UIButton *womanbutton = (UIButton *)[self.view viewWithTag:1005];
    [womanbutton setImage:[UIImage imageNamed:@"jw_sex_btn.png"] forState:UIControlStateNormal];
    if (sender.tag == 1004) {
        _user_sex = @"0";
        [sender setImage:[UIImage imageNamed:@"jw_sex_btn_sel.png"] forState:UIControlStateNormal];
        
    }else if (sender.tag == 1005){
        _user_sex = @"1";
        [sender setImage:[UIImage imageNamed:@"jw_sex_btn_sel.png"] forState:UIControlStateNormal];
    }
}
- (void)user_birthdayBtnOnclick:(UIButton *)sender{
    
    BirthdayVC *VC = [BirthdayVC new];
    
    VC.birthdayDateBlcok = ^(NSString *text){   // 1
        
//        NSLog(@"text is %@",text);
        
        self.cell.BirthdLabel.text = text;
        self.cell.BirthdLabel.textColor = [UIColor blackColor];
        
        _user_birthday = text;
        
    };
    
    [self.navigationController pushViewController:VC animated:YES];
    
}
- (void)user_ProfessionalButOnclick:(UIButton *)sender{
    
    if (sender.tag == 1006) {
        
        NSLog(@"111");
        
        JWAlertChoseButsView *view = [[JWAlertChoseButsView alloc]initWithFrame:CGRM(0, 0, 750-160, 400)];
        
        view.dataList = @[@"汽修行业",@"家电维修",@"家政服务",@"母婴亲子",@"互联网行业",@"培训行业",@"商务管理",@"建筑行业",@"销售行业"];
        
        view.layer.cornerRadius = 8.0;
        view.layer.masksToBounds = YES;
        
        view.ProfessionaDateBlcok = ^(NSString *text,NSMutableArray *arr){
            if (text.length > 0) {
                
                self.cell.ProfessionalLabel.text = text;
                self.cell.ProfessionalLabel.textColor = [UIColor blackColor];
                _user_occupation = text;
            }
        };
        
        _alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
        
        [self presentViewController:_alertController animated:YES completion:nil];
        
    }else if (sender.tag == 1007){
        
        NSLog(@"222");
        
        JWAlertChoseButsView *view = [[JWAlertChoseButsView alloc]initWithFrame:CGRM(0, 0, 750-160, 400)];
        
        view.dataList = @[@"总经理",@"业务经理",@"产品经理",@"项目经理",@"销售总监",@"运营主管",@"UI设计师",@"开发工程师",@"部门主管"];
        
        view.layer.cornerRadius = 8.0;
        view.layer.masksToBounds = YES;
        
        view.ProfessionaDateBlcok = ^(NSString *text,NSMutableArray *arr){
            
            if (text.length > 0) {
                
                self.cell.PostLabel.text = text;
                self.cell.PostLabel.textColor = [UIColor blackColor];
                _user_duties = text;
                
            }
        };
        
        _alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
        
        [self presentViewController:_alertController animated:YES completion:nil];
        
    }else if (sender.tag == 1008){
        
        NSLog(@"333");
        
        JWAlertChoseButsView *view = [[JWAlertChoseButsView alloc]initWithFrame:CGRM(0, 0, 750-160, 400)];
        
        view.StrOrArr = @"arr";
        view.dataList = @[@"丽人美发",@"服装装饰",@"家具器材",@"母婴亲子",@"酒店宾馆",@"游玩热点",@"进口商品",@"生鲜水果",@"家具器材"];
        
        view.layer.cornerRadius = 8.0;
        view.layer.masksToBounds = YES;
        
        view.ProfessionaDateBlcok = ^(NSString *text,NSMutableArray *arr){
            
            if (arr.count > 0) {
                
                NSString *string1 = [arr componentsJoinedByString:@"  "];
                
                self.cell.BusinessLabel.text = string1;
                self.cell.BusinessLabel.textColor = [UIColor blackColor];
                _user_business = text;
            }
            
        };
        
        _alertController = [TYAlertController alertControllerWithAlertView:view preferredStyle:TYAlertControllerStyleAlert];
        
        [self presentViewController:_alertController animated:YES completion:nil];
        
    }else if (sender.tag == 1009){
        
        NSLog(@"333");
        
    }else if (sender.tag == 1010){
        
        HomeAddressVC *VC = [HomeAddressVC new];
        
        VC.homeAddressBlcok  = ^(NSString *text,NSString *home_address,NSString *textID){   // 1

            //        NSLog(@"text is %@",text);

            self.cell.HomeAddressLabel.text = text;
            self.cell.HomeAddressLabel.textColor = [UIColor blackColor];
            _home_address = home_address;
            _home_region_id = textID;

        };
        
        [self.navigationController pushViewController:VC animated:YES];
        
    }else if (sender.tag == 1011){
        
        HSFLocationVC *vc = [[HSFLocationVC alloc]init];
        vc.delegate = self;
        [self.navigationController pushViewController:vc animated:YES];
        
    }
}
#pragma mark -HSFLocationVCDelegate
-(void)locationVC:(HSFLocationVC *)locationVC sureLocation:(NSString *)location lat:(NSString *)lat lng:(NSString *)lng province:(NSString *)province city:(NSString *)city county:(NSString *)county{
//    self.location = location;
//    self.lat = lat;
//    self.lng = lng;
//
//    self.province = province;
//    self.city = city;
//    self.area = county;
//    self.county = county;
    
    self.cell.LocaLabel.text = location;
    self.cell.LocaLabel.textColor = [UIColor blackColor];
    //刷新
//    [self.tableView reloadData];
}
#pragma mark -- 获取验证码
- (void)getCode:(NSString *)text currentBtn:(UIButton *)currentBtn
{
    [[DDNetworkManagerDate makeUrl_sendsms_with_user_phone:text type:@"1"] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"====>>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {

            NSDictionary *dic = responseObject;
            
            NSLog(@"====>>>%@",dic);
            currentBtn.enabled = NO;
            [currentBtn setTitle:[NSString stringWithFormat:@"剩余%zis", _timer] forState:UIControlStateNormal];
            _myTimer = [NSTimer scheduledTimerWithTimeInterval:1 block:^(NSTimer * _Nonnull timer) {
                [currentBtn setTitle:[NSString stringWithFormat:@"剩余%zis", _timer] forState:UIControlStateNormal];
                if (_timer == 0) {
                    [currentBtn setTitle:@"重新获取" forState:UIControlStateNormal];
                    currentBtn.enabled = YES;
                    [_myTimer invalidate];
                    _myTimer = nil;
                    _timer = KTime;
                }else{
                    _timer --;
                }
            } repeats:YES];
        }
    } failure:^(id errorObject) {
    }];
}

- (void)RegButOnclick:(UIButton *)sender{
    
    if (kStringIsEmpty(self.cell.PhoneTF.text)) {
        [self showBottomMessag:@"请输入手机号"];
        return;
    }else if (![NSString IsPhoneNumber:self.cell.PhoneTF.text]) {
        [self showBottomMessag:@"请输入正确的手机号"];
        return;
    }else if (kStringIsEmpty(self.cell.YanZhenMaTF.text)) {
        [self showBottomMessag:@"请输入验证码"];
        return;
    }else if (self.cell.YanZhenMaTF.text.length < 6) {
        [self showBottomMessag:@"请输入6位验证码"];
        return;
    }else if (kStringIsEmpty(self.cell.PassWordTF.text)) {
        [self showBottomMessag:@"请输入密码"];
        return;
    }else if (self.cell.PassWordTF.text.length < 6) {
        [self showBottomMessag:@"请输入6位密码"];
        return;
    }else if (kStringIsEmpty(self.cell.user_nicknameTF.text)) {
        [self showBottomMessag:@"请输入昵称"];
        return;
    }else if (kStringIsEmpty(self.cell.user_truenameTF.text)) {
        [self showBottomMessag:@"请输入姓名"];
        return;
    }
    
    [[DDNetworkManagerDate makeUrl_register_with_user_phone:self.cell.PhoneTF.text user_password:self.cell.PassWordTF.text code:self.cell.YanZhenMaTF.text user_nickname:self.cell.user_nicknameTF.text user_truename:self.cell.user_truenameTF.text user_sex:_user_sex user_birthday:_user_birthday user_occupation:_user_occupation user_duties:_user_duties user_business:_user_business user_hobby:self.cell.user_hobbyTF.text home_region_id:_home_region_id home_address:_home_address region_name:_region_name]post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"====>>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            [self showCenterMessage:@"注册成功"];
            
            [self goBack];
//            NSDictionary *dic = responseObject;
            
//            NSLog(@"====>>>%@",dic);
            
            
        }
    } failure:^(id errorObject) {
        
    }];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
